package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EsChangeForm;
import com.ruoyi.system.service.IEsChangeFormService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 换货单Controller
 *
 * @author ruoyi
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/system/change")
public class EsChangeFormController extends BaseController
{
    @Autowired
    private IEsChangeFormService esChangeFormService;

    /**
     * 查询换货单列表
     */
    @PreAuthorize("@ss.hasPermi('system:change:list')")
    @GetMapping("/list")
    public TableDataInfo list(EsChangeForm esChangeForm)
    {
        startPage();
        List<EsChangeForm> list = esChangeFormService.selectEsChangeFormList(esChangeForm);
        return getDataTable(list);
    }

    /**
     * 导出换货单列表
     */
    @PreAuthorize("@ss.hasPermi('system:change:export')")
    @Log(title = "换货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EsChangeForm esChangeForm)
    {
        List<EsChangeForm> list = esChangeFormService.selectEsChangeFormList(esChangeForm);
        ExcelUtil<EsChangeForm> util = new ExcelUtil<EsChangeForm>(EsChangeForm.class);
        util.exportExcel(response, list, "换货单数据");
    }

    /**
     * 获取换货单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:change:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(esChangeFormService.selectEsChangeFormById(id));
    }

    /**
     * 新增换货单
     */
    @PreAuthorize("@ss.hasPermi('system:change:add')")
    @Log(title = "换货单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EsChangeForm esChangeForm)
    {
        return toAjax(esChangeFormService.insertEsChangeForm(esChangeForm));
    }

    /**
     * 修改换货单
     */
    @PreAuthorize("@ss.hasPermi('system:change:edit')")
    @Log(title = "换货单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EsChangeForm esChangeForm)
    {
        return toAjax(esChangeFormService.updateEsChangeForm(esChangeForm));
    }

    /**
     * 删除换货单
     */
    @PreAuthorize("@ss.hasPermi('system:change:remove')")
    @Log(title = "换货单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(esChangeFormService.deleteEsChangeFormByIds(ids));
    }

    @PutMapping("/audit")
    @PreAuthorize("@ss.hasPermi('system:change:audit')")
    public AjaxResult audit(@RequestBody EsChangeForm form) {
        // 验证当前状态
        EsChangeForm exist = esChangeFormService.selectEsChangeFormById(form.getId());
        if (exist.getStatus() != 1) {
            return AjaxResult.error("只有待审核状态的单据可以审核");
        }

        // 设置审核时间
        form.setAuditTime(new Date());

        // 如果审核通过，清空拒绝原因
        if(form.getStatus() == 2) {
            form.setRejectReason(null);
        }

        return toAjax(esChangeFormService.auditEsChangeForm(form));
    }

    /**
     * 提交换货单
     */
    @PreAuthorize("@ss.hasPermi('system:change:edit')")
    @Log(title = "换货单", businessType = BusinessType.UPDATE)
    @PutMapping("/submit/{id}")
    public AjaxResult submit(@PathVariable Long id)
    {
        EsChangeForm esChangeForm = new EsChangeForm();
        esChangeForm.setId(id);
        esChangeForm.setStatus(1L); // 设置为待审核状态
        esChangeForm.setSubmitTime(DateUtils.getNowDate());
        return toAjax(esChangeFormService.submitEsChangeForm(esChangeForm));
    }
}
